﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DalatCooking.Core.Helper
{
    public class ConvertObject
    {
        public static int ToInt(object obj, int defaultValue)
        {
            try { return int.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static int ToInt(object obj)
        {
            return ToInt(obj, -1);
        }
        public static double ToDouble(object obj, double defaultValue)
        {
            try { return double.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static double ToDouble(object obj)
        {
            return ToDouble(obj, -1);
        }
        public static float ToFloat(object obj, float defaultValue)
        {
            try { return float.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static float ToFloat(object obj)
        {
            return ToFloat(obj, -1);
        }
        public static short ToShort(object obj, short defaultValue)
        {
            try { return short.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static short ToShort(object obj)
        {
            return ToShort(obj, -1);
        }
        public static long ToLong(object obj, long defaultValue)
        {
            try { return long.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static long ToLong(object obj)
        {
            return ToLong(obj, -1);
        }
        public static string ToString(object obj, string defaultValue)
        {
            try { return obj.ToString(); }
            catch { return defaultValue; }
        }
        public static string ToString(object obj)
        {
            return ToString(obj, "");
        }
        public static DateTime ToDateTime(object obj, DateTime defaultValue)
        {
            try { return DateTime.Parse(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static DateTime ToDateTime(object obj)
        {
            return ToDateTime(obj, DateTime.MinValue);
        }
        public static bool ToBool(object obj, bool defaultValue)
        {
            try { return Convert.ToBoolean(obj.ToString()); }
            catch { return defaultValue; }
        }
        public static bool ToBool(object obj)
        {
            return ToBool(obj, false);
        }
    }
}
